function parseSearch() {
  const params = new URLSearchParams(location.search.slice(1)); // 去掉第一个问号

  // 将参数转换成对象
  const obj = {};
  for (let param of params) {
    obj[param[0]] = param[1];
  }
  return obj;
}

const params = parseSearch();

// 倒计时
const expireDom = document.querySelector(".footer .expire");
let timerId = setInterval(() => {
  const time = expireDom.innerText - 1;
  expireDom.innerText = time;
  if (+time <= 0) {
    clearInterval(timerId);
    timerId = null;
    location.href = "/";
  }
}, 1000);

let socket = io();

// 首先判断订单是否已经支付
socket.emit("checkPay", params.orderId);
socket.on("payed", (data) => {
  if (data) {
    location.href = params.return;
  }
});

/**
 * 监听支付成功
 */
socket.on("paySuccess", (money) => {
  location.href = params.return;
});

/**
 * 控制支付金额
 */
const moneyDom = document.querySelector(".money-wrapper .money");
// const randomMoney = [9.4, 9.5, 9.6, 9.7, 9.8, 9.9];
const randomMoney = [19.4, 19.5, 19.6, 19.7, 19.8, 19.9];

function getRandom(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}
moneyDom.innerText = randomMoney[getRandom(0, randomMoney.length)];
